package airacrobats.gear;

import airacrobats.gui.container.MainMenuContainer;
import airacrobats.io.data.StageData;
import airacrobats.game.Squadron;
import airacrobats.game.Stage;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.SwingUtilities;

/**
 * The menu is where the player chooses the stages and stuff.
 */
public class MenuGear extends Gear implements ActionListener
{
    /**
     * Main menu container.
     */
    private MainMenuContainer mainMenuContainer;
    /**
     * Selected squadron.
     */
    private Squadron selectedSquadron;
    /**
     * Stages.
     */
    private List<Stage> stages;
    /**
     * Flag indicating if this gear was already loaded.
     */
    private boolean loaded;

    /**
     * @param gearListener The gear listener.
     * @param selectedSquadron The selected squadron.
     */
    public MenuGear(GearListener gearListener, Squadron selectedSquadron)
    {
        super(gearListener);

        this.selectedSquadron = selectedSquadron;
        
        stages = new ArrayList<Stage>();
    }

    @Override
    public boolean closing()
    {
        if (scoreLoader != null)
        {
            try
            {
                scoreLoader.save(selectedSquadron, stages);
            }
            catch (Exception ex)
            {
                Thread.getDefaultUncaughtExceptionHandler().uncaughtException(null, ex);
            }
        }

        return super.closing();
    }

    /**
     */
    public void load()
    {
        if (loaded)
        {
            mainMenuContainer.resetGaming();
        }
        else
        {
            // Creates the stages.
            int index = 0;
            for (StageData stageData : stageLoader.getObjects())
            {
                int scoreRecord = scoreLoader.get(selectedSquadron.getType().getMapKey()
                        + "/Scores.dat_" + index);
                BufferedImage image = imageLoader.get(selectedSquadron.getType().getMapKey()
                        + "/Stage" + index++ + ".jpg");
                stages.add(new Stage(stageData, image, scoreRecord));
            }

            mainMenuContainer = new MainMenuContainer(this, stages);

            GridBagConstraints c = new GridBagConstraints();

            c.gridx = 0;
            c.gridy = 0;
            add(mainMenuContainer, c);

            loaded = true;
        }
    }

    /**
     */
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == mainMenuContainer.getPlayButton())
        {
            mainMenuContainer.startGaming();

            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    Gear gear = new GameGear(MenuGear.this.gearListener, MenuGear.this, selectedSquadron,
                            mainMenuContainer.getSelectedStage());
                    gear.load();
                    gearListener.gearChanging(gear);
                }
            });
        }
    }
}
